import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.util.List;

public class CsvUtil {

    public static void  writeCsv(List list,String dateStr){
        try {
            try {
                Long start = System.currentTimeMillis();
                File file=new File("C:\\Users\\zhoujuqi\\Desktop\\"+dateStr+".csv");
                FileOutputStream fos=new FileOutputStream(file,true);
                OutputStreamWriter osw=new OutputStreamWriter(fos, "UTF-8");
                BufferedWriter bw=new BufferedWriter(osw);
                String linesd= "["+list.toString()+"]";
                String info =linesd.replace("\"", "");
                String item[]  = info.split(";,");
                for(String arr:item){
                    String arrs=arr.substring(0,arr.length()-1);
                    bw.write(arrs+"\t\n");
                }
                //注意关闭的先后顺序，先打开的后关闭，后打开的先关闭
                bw.close();
                osw.close();
                fos.close();
                long end = System.currentTimeMillis();
                System.out.println("完成"+dateStr+"入库，耗时：" + (end - start) +" ms");
            }catch (Exception e){
            }

        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void writeCSV(List<Asn> list) {
        // 定义一个CSV路径
        /*String csvFilePath = "C:\\Users\\zhoujuqi\\Desktop\\asn.csv";
        try {
            // 创建CSV写对象 例如:CsvWriter(文件路径，分隔符，编码格式);
            CsvWriter csvWriter = new CsvWriter(csvFilePath, ',', Charset.forName("UTF-8"));
            // 写表头
            String[] csvHeaders = { "id", "asnNo", "purchaseNo", "deliverySign", "deliverySignKey", "consigneeSign", "consigneeSignKey" };
            csvWriter.writeRecord(csvHeaders);
            // 写内容
            for (Asn item : list) {
                String[] csvContent = {item.getId().toString() , item.getAsnNo(), item.getPurchaseNo(), item.getDeliverySign(), item.getDeliverySignKey(), item.getConsigneeSign(), item.getConsigneeSignKey() };
                csvWriter.writeRecord(csvContent);
            }
            csvWriter.close();
            System.out.println("--------CSV文件已经写入--------");
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }
}
